﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using SharedGamesClasses;

namespace GuiGames {

    /// <summary>
    /// Provides a GUI for the Two Up game in the SharedGamesClasses.
    /// </summary>
    public partial class TwoUpForm : Form {

        private Games MainGame;
        private ListBoxTraceListener listBoxTraceListener;
        private Timer timer = new Timer();
        private int deadline = 10;
        private bool head = true;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="form"></param>
        public TwoUpForm(Games _game)
        {
            InitializeComponent();            
            this.MainGame = _game;
            TwoUp.ResetGlobals();
            timer.Tick+=new EventHandler(timer_Tick);
            InitForm();
        }


        /// <summary>
        /// Throws the two coins and shows the results on the screen.
        /// </summary>
        private void ShowResultsOfThrow() {
            // Code needed.
                     
            switch (TwoUp.Play())
            {
                case 0:
                    lblResult.Text = "Tails";
                    SetPictureBox(0, false);
                    SetPictureBox(1, false);
                    btnPlayAgain.Visible = true;
                    break;
                case 1:
                    lblResult.Text = "Odds";
                    if (TwoUp.IsHeads(0))
                    {
                        SetPictureBox(0, true);
                        SetPictureBox(1, false);
                    }
                    else
                    {
                        SetPictureBox(0, false);
                        SetPictureBox(1, true);
                    }
                    btnPlayAgain.Visible = false;
                    btnThrowCoins.Enabled = true;
                    break;
                case 2:
                    lblResult.Text = "Heads";
                    SetPictureBox(0, true);
                    SetPictureBox(1, true);
                    btnPlayAgain.Visible = true;
                    break;
                default:
                    break;
            }

            lblResult.Visible = true;                      
        }

        private void InitForm()
        {
            listBoxTraceListener = new ListBoxTraceListener(twoUpListBox);
            Trace.Listeners.Add(listBoxTraceListener);
            btnPlayAgain.Visible = false;
            lblResult.Visible = false;
            SetPictureBox(0, true);
            SetPictureBox(1, true);
        }

        /// <summary>
        /// Sets the image in the PictureBox for the given coinNum
        /// to Images.Heads if the Heads parameter is True,
        /// or Images.Tails if the Heads parameter is False.
        /// </summary>
        private void SetPictureBox(int coinNum, bool Heads) {
            if (coinNum == 0)
            {
                ptbLeftCoin.Image = Images.GetCoinImage(Heads);
            }
            else
            {
                ptbRightCoin.Image = Images.GetCoinImage(Heads);
            }
        }

        private void btnThrowCoins_Click(object sender, EventArgs e)
        {
            btnThrowCoins.Enabled = false; 
            timer.Enabled = true;            
        }

        private void btnPlayAgain_Click(object sender, EventArgs e)
        {
            btnPlayAgain.Visible = false;
            btnThrowCoins.Enabled = true;
            lblResult.Visible = false;
            SetPictureBox(0, true);
            SetPictureBox(1, true);
        }

        private void btnCancelGame_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            MainGame.Show();
        }

        private void timer_Tick(object sender, EventArgs e)
        {

            timer.Interval = 100;
            head = head == true ? false : true;
            SetPictureBox(0, head);
            SetPictureBox(1, !head);
            deadline--;
            if (deadline == 0)
            {
                timer.Enabled = false;
                deadline = 10;
                ShowResultsOfThrow();
            }

        }
    } //end class TwoUpForm
} // end namespace
